package com.pmenergy.control;

// Generated 15/08/2014 01:13:31 AM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pmenergy.model.Cliente;


/**
 * Home object for domain model class Cliente.
 * @see com.pmenergy.model.Cliente
 * @author Hibernate Tools
 */
@Stateless
public class ClienteHome {

	private static final Log log = LogFactory.getLog(ClienteHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public int persist(Cliente transientInstance) {
		int result=1;
		log.debug("persisting Cliente instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
			return result;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			return -1;
		}
	}

	public void remove(Cliente persistentInstance) {
		log.debug("removing Cliente instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Cliente merge(Cliente detachedInstance) {
		log.debug("merging Cliente instance");
		try {
			Cliente result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Cliente findById(BigDecimal id) {
		log.debug("getting Cliente instance with id: " + id);
		try {
			Cliente instance = entityManager.find(Cliente.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public int removeById(BigDecimal id) {
		log.debug("removing Cliente instance with id: " + id);
		try {
			int deletedClientes = entityManager.createQuery("DELETE FROM Cliente c WHERE c.clienteId = :id")
					.setParameter("id",id).executeUpdate();
			log.debug("get successful");
			return deletedClientes;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<Cliente> findAllClientes() {
		log.debug("getting all Clientes instances " );
		try {
			
			List<Cliente> list = entityManager.createQuery("SELECT c FROM Cliente c ").getResultList();
			log.debug("get successful");
			return list;
		} 
		/*catch (NoResultException re){
			return null;
		}*/
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<Cliente> findClientesByFields(String nombre,String direccion,String ruc,String ciudad,String pais,String contacto) 
			throws RuntimeException{
		log.debug("getting all Clientes instances " );
		try {
			Query query;
			String q="SELECT c FROM Cliente c  WHERE "
					+  "c.nombre LIKE :nombre  "
					+  "AND c.direccion LIKE :direccion "
					+  "AND c.ruc LIKE :ruc "
					+  "AND c.ciudad LIKE :ciudad "
					+  "AND c.pais LIKE :pais "
					+  "AND c.contacto LIKE :contacto";
			
			
			query = entityManager.createQuery(q);	
				
			query.setParameter("nombre","%" + nombre + "%" );
			query.setParameter("direccion","%" + direccion + "%");
			query.setParameter("ruc","%" + ruc + "%");																	
			query.setParameter("ciudad","%" + ciudad + "%");
			query.setParameter("pais","%" + pais + "%");																	
			query.setParameter("contacto","%" + contacto + "%");
		
			
			List<Cliente> list = query.getResultList();
			log.debug("get successful");
			
			return list;
		} 
		/*catch (NoResultException re){
			return null;
		}*/
		catch (RuntimeException re) {
			log.error("get failed", re);
			return null;
			//throw re;
			
		}
	}
	
	public int updateCliente(BigDecimal clienteId,String nombre,String direccion,String ruc,String ciudad,String pais,String telefono1,String telefono2,String contacto,String email) {
		
		int updatedCliente;
		log.debug("updating Cliente instance " );
		try {
			
			Query query;
			query = entityManager.createQuery("UPDATE Cliente c SET c.nombre = :nombre,"
																				+  "c.telefono1 = :telefono1,"
																				+  "c.telefono2 = :telefono2,"
																				+  "c.email = :email,"
																				+  "c.direccion = :direccion,"
																				+  "c.ruc = :ruc,"
																				+  "c.ciudad = :ciudad,"
																				+  "c.pais = :pais,"
																				+  "c.contacto = :contacto "
																				+ " WHERE c.clienteId = :id");
			
																				
			query.setParameter("id",clienteId);
			query.setParameter("nombre",nombre);
			query.setParameter("direccion",direccion);
			query.setParameter("ruc",ruc);
			query.setParameter("ciudad",ciudad);
			query.setParameter("pais",pais);
			query.setParameter("telefono1",telefono1);																	
			query.setParameter("telefono2",telefono2);
			query.setParameter("contacto",contacto);
			query.setParameter("email",email);
			
			updatedCliente = query.executeUpdate();
			return updatedCliente;
		}			
		catch (RuntimeException re) {
			log.error("get failed", re);				
			return -1;
			
		}
	}

	
}
